package com.itic.system.carrage.templateRegion.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itic.appbase.framework.type.RegionLevel;
import com.itic.system.carrage.templatePiece.persistence.model.CarrageTemplatePiece;
import com.itic.system.region.persistence.model.HppRegion;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.common.util.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.system.carrage.templateRegion.persistence.dao.CarrageTemplateRegionDao;
import com.itic.system.carrage.templateRegion.persistence.model.CarrageTemplateRegion;
import com.itic.system.carrage.templateRegion.persistence.model.CarrageTemplateRegionExample;
import com.itic.system.carrage.templateRegion.persistence.model.Condition;
import com.itic.system.region.service.HppRegionService;

@Service
@Transactional
public class CarrageTemplateRegionService
     extends CrudService<CarrageTemplateRegionDao,CarrageTemplateRegion,CarrageTemplateRegionExample> {
    @Autowired
    private CarrageTemplateRegionDao carrageTemplateRegionDao;
    @Autowired 
    private HppRegionService hppRegionService;


    /**
     * 区域添加
     * @param templateId
     * @param pId
     * @param regionIds
     * @return
     */
    public String add(String templateId,String pId,String[] regionIds){
    	String result = FAILED;
    	if(StringHelper.isNotBlank(templateId) && StringHelper.isNotBlank(pId)
    			&& regionIds != null){
    		for(int i=0;i<regionIds.length;i++){
        		String regionId = regionIds[i];
        		String regionName = hppRegionService.getNameById(regionId);
    			CarrageTemplateRegion region = new CarrageTemplateRegion();
        		region.setCarrageTemplateId(templateId);
        		region.setPieceId(pId);
        		region.setRegionName(regionName);
        		region.setRegionId(regionId);
        		result = this.add(region);
        	}
    	}
    	if(StringHelper.isNotBlank(templateId) && StringHelper.isNotBlank(pId)
    			&& (regionIds == null || regionIds.length == 0) ){
    		result = SUCCESS;
    	}
    	return result;
    }
    /**
     * 根据模板id和续重id查数据
     * @param templateId
     * @param pieceId
     * @return
     */
    public List<String> getBypieceId(Condition condition){
    	List<String> list = this.carrageTemplateRegionDao.getBypieceId(condition);
    	return list;
    }
    /**
     * 获取区域名称
     * @param condition
     * @return
     */
    public String getRegionName(Condition condition){
    	String name = "";
    	List<CarrageTemplateRegion> list = this.carrageTemplateRegionDao.getRegion(condition);
    	for(int i=0;i<list.size();i++){
    		if(StringHelper.isBlank(name) && list.size()>1 ){
    			name = list.get(i).getRegionName()+",";
    		}else if(StringHelper.isBlank(name) && list.size()== 1 ){
    			name = list.get(i).getRegionName();
    		}else if(StringHelper.isNotBlank(name) && i != list.size()-1){
    			name = name + list.get(i).getRegionName() + ",";
    		}else if(StringHelper.isNotBlank(name) && i == list.size()-1){
    			name = name + list.get(i).getRegionName();
    		}
    	}
    	return name;
    }
    
    public void delBytemplateId(String templateId){
    	this.carrageTemplateRegionDao.delBytemplateId(templateId);
    }

	/**
	 * 根据运费模板Id和地区Id查询模板地区
	 * @param templateId
	 * @param regionId
	 * @return
	 */
    public CarrageTemplateRegion getCarrageTemplateRegionByTemplateIdAndRegionId(String templateId,String regionId){
    	Map<String,Object> params = new HashMap<>();
    	params.put("templateId",templateId);
    	params.put("regionId",regionId);
		CarrageTemplateRegion carrageTemplateRegion = this.carrageTemplateRegionDao.getCarrageTemplateRegionByTemplateIdAndRegionId(params);
		return carrageTemplateRegion;
	}

	/**
	 * 获取某个地区是否还有运费模板 （千万不要动这个）
	 * @param templateId
	 * @param regionId
	 * @return
	 */
	public CarrageTemplateRegion  recurCarrageTemplateRegion(String templateId,String regionId){
		HppRegion region = this.hppRegionService.get(regionId);
		CarrageTemplateRegion carrageTemplateRegion = this.getCarrageTemplateRegionByTemplateIdAndRegionId(templateId,region.getId());
		if(carrageTemplateRegion == null && StringUtils.isNotEmpty(region.getParentRegionId())){
			region = this.hppRegionService.get(region.getParentRegionId());
			carrageTemplateRegion = this.getCarrageTemplateRegionByTemplateIdAndRegionId(templateId,region.getId());
			if(carrageTemplateRegion == null && StringUtils.isNotEmpty(region.getParentRegionId())){
				region = this.hppRegionService.get(region.getParentRegionId());
				carrageTemplateRegion = this.getCarrageTemplateRegionByTemplateIdAndRegionId(templateId,region.getId());
			}
		}
		return carrageTemplateRegion;
	}


}